#include <stdio.h>
#include "../inc/uthash.h"

typedef struct {
    char name[100];
    UT_hash_handle hh;
} User;

User *g_userList = NULL;

void AddUser(char *n)
{
    User *tmp = NULL;
    HASH_FIND_STR(g_userList, n, tmp);
    if (tmp == NULL) {
        tmp = (User *)malloc(sizeof(User));
        strcpy(tmp->name, n);
        HASH_ADD_STR(g_userList, name, tmp);
        printf("Add: %s\n", tmp->name);
    } else {
        printf("ADD: %s, exist\n", tmp->name);
    }
}

void PrintList()
{
    User *el = NULL;
    User *tmp = NULL;
    printf("Print cnt: %d\n", HASH_COUNT(g_userList));
    HASH_ITER(hh, g_userList, el, tmp)
    {
        printf("Print : %s\n", el->name);
    }
}

void FreeList()
{
    User *el = NULL;
    User *tmp = NULL;
    printf("Delete cnt: %d\n", HASH_COUNT(g_userList));
    HASH_ITER(hh, g_userList, el, tmp)
    {
        printf("Print : %s\n", el->name);
        HASH_DEL(g_userList, el);
        free(el);
    }
}

int main()
{
    AddUser("wangtao");
    AddUser("yangpei");
    PrintList();
    FreeList();
    PrintList();
    return 0;
}